package com.yunxin.admin.system.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunxin.core.customer.domain.CustomerInfo;
import com.yunxin.core.customer.service.CustomerInfoService;
import com.yunxin.core.utils.infra.support.lock.DistributedLock;
import com.yunxin.core.utils.infra.support.lock.DistributedLockFactory;
import com.yunxin.core.utils.infra.support.lock.TaskLockKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
@DS("crm")
public class SeasTask {

    @Value("${seas.scan.time}")
    private Long scanTime;

    @Autowired
    private CustomerInfoService customerInfoService;

    private final DistributedLockFactory distributedLockFactory;
    //公海池判断
    @Scheduled(cron = "0/10 * * * * *")
    @Transactional(rollbackFor = Exception.class)
    public void seasTask() {
        try {
//            OsInfo osInfo = SystemUtil.getOsInfo();
//            if (osInfo.isWindows()) {
//                return;
//            }
            TaskLockKey taskLockKey = TaskLockKey.seas_LOCK_KEY;
            DistributedLock lock = distributedLockFactory.provideDistributedLock(taskLockKey.getPrefix());
            boolean tryLock = lock.tryLock(taskLockKey.getWaitTime(), taskLockKey.getReleaseTime(), taskLockKey.getTimeUnit());
            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            if (tryLock){
                try {
                    System.out.println("seasTask定时任务开始了 = " + DateUtil.date());
                    Date nowDate=new Date();
                    //Date lateDate=new Date(nowDate.getTime()-1000*60*60*48);//48小时
                    Date lateDate=new Date(nowDate.getTime()-scanTime);//48小时
                    LambdaUpdateWrapper<CustomerInfo> updateWrapper=new LambdaUpdateWrapper<>();
                    //话务员未跟进 telephoner_status话务员状态0   telephoner_time分配时间超过48小时后
                    updateWrapper.ne(CustomerInfo::getAbutmentType,4)
                            .eq(CustomerInfo::getSeasType,0)
                            .eq(CustomerInfo::getTelephonerStatus,0)
                            .eq(CustomerInfo::getIsWechat,0)
                            .le(CustomerInfo::getTelephonerTime,lateDate)
                            .set(CustomerInfo::getSeasType,1)
                            .set(CustomerInfo::getSeasDate,new Date());
                    customerInfoService.update(updateWrapper);
                    //telephoner_status话务员状态不等于3的 last_follow_time话务员最后跟进时间超过48小时后
                    updateWrapper.clear();
                    updateWrapper.ne(CustomerInfo::getAbutmentType,4)
                            .eq(CustomerInfo::getSeasType,0)
                            .ne(CustomerInfo::getTelephonerStatus,0)
                            .eq(CustomerInfo::getIsWechat,0)
                            .le(CustomerInfo::getLastFollowTime,lateDate)
                            .set(CustomerInfo::getSeasType,1)
                            .set(CustomerInfo::getSeasDate,new Date());
                    customerInfoService.update(updateWrapper);
                    //telephoner_status话务员状态等于3 和 isWechat==0 的 last_follow_time话务员最后跟进时间超过48小时后
//                    updateWrapper.clear();
//                    updateWrapper.ne(CustomerInfo::getAbutmentType,4)
//                            .eq(CustomerInfo::getSeasType,0)
//                            .eq(CustomerInfo::getTelephonerStatus,3)
//                            .eq(CustomerInfo::getIsWechat,0)
//                            .le(CustomerInfo::getLastFollowTime,lateDate)
//                            .set(CustomerInfo::getSeasType,1)
//                            .set(CustomerInfo::getSeasDate,new Date());
//                  customerInfoService.update(updateWrapper);
                    //企微客户 48小时未添加 进入公海池
                    updateWrapper.clear();
                    updateWrapper.eq(CustomerInfo::getAbutmentType,4)
                            .eq(CustomerInfo::getSeasType,0)
                            .eq(CustomerInfo::getIsWechat,0)
                            .le(CustomerInfo::getApplyTime,lateDate)
                            .set(CustomerInfo::getSeasType,1)
                            .set(CustomerInfo::getSeasDate,new Date());
                    customerInfoService.update(updateWrapper);
                    //
                }finally {
                    if (lock.isHeldByCurrentThread()) {
                        lock.unlock();
                    }
                }
            }else {
                log.info("ip:{},执行TeamTask任务失败,获取锁失败......", hostAddress);
            }
        }catch (Exception e) {
            log.error("执行TeamTask任务异常", e);
        }
    }
}




